package com.sky.task;

import com.sky.entity.Orders;
import com.sky.mapper.OrderMapper;
import com.sky.service.OrderService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;

import java.time.LocalDate;
import java.time.LocalDateTime;
import java.util.List;

@Component
@Slf4j
public class OrderTask {

    @Autowired
    private OrderMapper orderMapper;
    //处理超时15分钟的订单。每分钟执行一次
   // @Scheduled(cron="* 0/1 * * * ? ")
    //@Scheduled(cron="0/5 * * * * ? ")
    public void handleTimeoutOrder() {
        log.info("开始处理超时15分钟的订单...{}", LocalDateTime.now());
        //状态，超时
        LocalDateTime time = LocalDateTime.now().plusMinutes(-15);
        List<Orders>ordersList = orderMapper.getByStatusAndOrderTimeLT(Orders.PAID,time);

        if (ordersList.size()>0&&ordersList!=null){
            for (Orders orders : ordersList) {
                orders.setStatus(Orders.COMPLETED);
                orders.setCancelReason("订单超时未付款");
                orders.setCancelTime(LocalDateTime.now());
                orderMapper.update(orders);
            }
        }
    }

    //处理一直未配送完成的订单，每天凌晨1点执行一次
    //@Scheduled(cron="* * 1 * * ? ")
    //@Scheduled(cron="1/5 * * * * ? ")
    public void handleUnfinishedOrder() {
        log.info("开始处理一直未配送完成的订单...{}",LocalDateTime.now());
        List<Orders> ordersList=orderMapper.getByStatus(Orders.DELIVERY_IN_PROGRESS);

        if (ordersList.size()>0&&ordersList!=null){
            for (Orders orders : ordersList) {
                orders.setStatus(Orders.COMPLETED);
                orderMapper.update(orders);
            }
        }
    }

}
